package com.example.peibei.ui.activity;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dingtao.common.bean.BillRecord;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.BillRecordPresenter;
import com.example.peibei.ui.adapter.BillRecordAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class BillRecordActivity extends WDActivity {
    @BindView(R.id.rv_bill)
    RecyclerView rv_bill;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private BillRecordPresenter billRecordPresenter;

    private SPUtils spUtils;
    private String token;
    private List<BillRecord.RecordsDTO> bills = new ArrayList<>();
    private BillRecordAdapter billRecordAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bill_record;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(BillRecordActivity.this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        rv_bill.setLayoutManager(new LinearLayoutManager(this));
        billRecordAdapter = new BillRecordAdapter(bills, BillRecordActivity.this);
        rv_bill.setAdapter(billRecordAdapter);
        billRecordPresenter = new BillRecordPresenter(new BillRecordCall());
        onRefreshStart();
        refreshLayout.setOnRefreshListener(refreshLayout -> onRefreshStart());
        billRecordAdapter.setOnDetailClickListener(new BillRecordAdapter.OnDetailClickListener() {
            @Override
            public void onClick(int position, String logId) {
                Bundle bundle = new Bundle();
                bundle.putString("logId", logId);
                intent(BillRecordDetailActivity.class, bundle);
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    public void onRefreshStart() {
        if (billRecordPresenter.isRunning()) {
            refreshLayout.finishRefresh();
            return;
        }
        billRecordPresenter.reqeust(true, "Bearer " + token);
    }

    class BillRecordCall implements DataCall<BillRecord> {

        @Override
        public void success(BillRecord data, Object... args) {
            refreshLayout.finishRefresh();
            //添加列表并刷新
            if (billRecordPresenter.getPage() == 1) {
                bills.clear();
            }
            bills.addAll(data.getRecords());
            billRecordAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    @Override
    protected void destoryData() {

    }
}
